package cn.sirun.com.fca.personal.center.view.impl;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.tencent.cloud.iov.common.flow.view.impl.BaseLoadingFlowView;
import com.tencent.cloud.iov.common.flow.view.impl.BaseLoadingRecyclerFlowView;
import com.tencent.cloud.iov.flow.view.IContentViewContainer;
import com.tencent.cloud.iov.kernel.IovKernel;
import com.tencent.cloud.iov.kernel.constant.StatusCode;
import com.tencent.cloud.iov.kernel.model.BaseResponse;
import com.tencent.cloud.iov.router.Router;
import com.tencent.cloud.iov.util.ActivityUtils;
import com.tencent.cloud.iov.util.ResourcesUtils;
import com.tencent.cloud.iov.util.lifecycle.ActivityStackManager;
import com.tencent.cloud.iov.util.storage.TXSharedPreferencesUtils;
import com.tencent.cloud.uikit.ui.widget.IovButton;

import cn.sirun.com.fca.account.constant.CarConst;
import cn.sirun.com.fca.account.model.base.CarInfoDetailResponse;
import cn.sirun.com.fca.account.model.login.PersonalInfo;
import cn.sirun.com.fca.account.model.login.PersonalInfoRequest;
import cn.sirun.com.fca.account.service.LoginManager;
import cn.sirun.com.fca.account.uitl.CurCarInfoUtils;
import cn.sirun.com.fca.account.uitl.PersonalInfoUtils;
import cn.sirun.com.fca.personal.center.R;
import cn.sirun.com.fca.personal.center.constant.PersonalCenterActivityConst;
import cn.sirun.com.fca.personal.center.model.service.PersonalInfoManager;
import cn.sirun.com.fca.personal.center.presenter.IPersonalCenterPresenter;
import cn.sirun.com.fca.personal.center.presenter.impl.PersonalCenterPresenter;
import cn.sirun.com.fca.personal.center.view.IPersonalCenterView;

public class PersonalCenterFlowView extends BaseLoadingFlowView<IPersonalCenterPresenter>
        implements IPersonalCenterView, View.OnClickListener {

    private TextView mMailTV, mPhoneTV, mNameTV, mVinTV, mCarStatusTV;
    private IovButton mEditBtn;
    private RelativeLayout mCarRl;
    private LinearLayout mTelLL, mSystemSettingLL, mNoticeSettingLL, mCarLL, mTrafficLL;

    private PersonalInfo mPersonInfo;

    public PersonalCenterFlowView(Activity activity) {
        this(activity, null);
    }

    public PersonalCenterFlowView(Activity activity, IContentViewContainer container) {
        super(activity, container);
    }

    @Override
    public void onPersonalInfoResult(BaseResponse<PersonalInfo> response) {
        int code = response.getCode();
        if (code == StatusCode.SUCCESS) {
            mPersonInfo = response.getData();
            handlePersonalInfoResult();
        }
    }

    @Override
    public void onCarInfoResult(BaseResponse<CarInfoDetailResponse> response) {
        int code = response.getCode();
        if (code == StatusCode.SUCCESS) {
            handleCarInfoResult(response.getData());
        }
    }

    private void handleCarInfoResult(CarInfoDetailResponse carInfo) {
        if (carInfo.getReseller() != null) {
            mPersonInfo.setReseller(carInfo.getReseller());
            PersonalInfoUtils.savePersonalInfo(mPersonInfo);
        }
        CurCarInfoUtils.saveCarInfo(carInfo);
        initData();
    }

    private void handlePersonalInfoResult() {
        if (!TextUtils.isEmpty(mPersonInfo.getIovCurrentVehicle())) {
            TXSharedPreferencesUtils.setValue(CarConst.SpKey.CAR_ACTIVATE_INFO,
                    mPersonInfo.getIovCurrentVehicle());
            getPresenter().getCarInfo(mPersonInfo.getIovCurrentVehicle());
            PersonalInfoUtils.savePersonalInfo(mPersonInfo);
        } else {
            initData();
        }
    }

    @Override
    protected IPersonalCenterPresenter createPresenter() {
        return new PersonalCenterPresenter();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        initView();
    }

    @Override
    public void onStart() {
        super.onStart();
        PersonalInfoRequest request = new PersonalInfoRequest(
                IovKernel.getQQAppId(), IovKernel.getWeChatAppId()
        );
        getPresenter().getPersonalInfo(request);
    }

    private void initView() {
        View view = LayoutInflater.from(getContext())
                .inflate(R.layout.personal_center_layout, null, false);
        setContentView(view);
        view.setBackgroundResource(R.drawable.page_bg_gradient);
        mEditBtn = view.findViewById(R.id.btn_profile_edit);
        mMailTV = view.findViewById(R.id.tv_profile_mail);
        mPhoneTV = view.findViewById(R.id.tv_profile_phone);
        mNameTV = view.findViewById(R.id.tv_car_name);
        mVinTV = view.findViewById(R.id.tv_car_vin);
        mCarStatusTV = view.findViewById(R.id.tv_cur_car);
        mCarRl = view.findViewById(R.id.rl_car_layout);
        mTelLL = view.findViewById(R.id.ll_tel);
        mSystemSettingLL = view.findViewById(R.id.ll_system_setting);
        mNoticeSettingLL = view.findViewById(R.id.ll_notice_setting);
        mCarLL = view.findViewById(R.id.ll_car);
        mTrafficLL = view.findViewById(R.id.ll_traffic);

        mEditBtn.setOnClickListener(this);
        mCarRl.setOnClickListener(this);
        mTelLL.setOnClickListener(this);
        mSystemSettingLL.setOnClickListener(this);
        mNoticeSettingLL.setOnClickListener(this);

        initData();
    }

    private void initData() {
        PersonalInfo personalInfo = PersonalInfoUtils.getPersonalInfo();
        if (personalInfo != null) {
            if (TextUtils.isEmpty(personalInfo.getEmail())) {
                mMailTV.setVisibility(View.GONE);
            } else {
                mMailTV.setText(personalInfo.getEmail());
            }

            if (TextUtils.isEmpty(personalInfo.getUserPhone())) {
                mPhoneTV.setVisibility(View.GONE);
            } else {
                String userPhone = personalInfo.getUserPhone();
                userPhone = userPhone.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
                mPhoneTV.setText(userPhone);
            }

            if (TextUtils.isEmpty(personalInfo.getIovCurrentVehicle())) {
                mCarLL.setVisibility(View.GONE);
                mTrafficLL.setVisibility(View.GONE);
            } else {
                mCarLL.setVisibility(View.VISIBLE);
            }
        }
        CarInfoDetailResponse carInfo = CurCarInfoUtils.getCarInfo();
        if (carInfo != null) {
            mNameTV.setText("Alfaromeo");
            mVinTV.setText(carInfo.getVin());
            int availableStatus = carInfo.getAvailableStatus();
            if (availableStatus == 0) {
                mCarStatusTV.setText(R.string.id_not_verified);
            } else if (availableStatus == 1) {
                mCarStatusTV.setText(R.string.verifying);
            } else if (availableStatus == 2) {
                mCarStatusTV.setText(R.string.profile_car);
            }
        }
    }

    private void startActivityForResult(String path) {
        Intent intent = Router.fromPath(path);
        ActivityUtils.startActivityForResult(getActivity(), intent);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.btn_profile_edit) {
            startActivityForResult(PersonalCenterActivityConst.Path.PERSONAL_OPTIONS);
        } else if (id == R.id.rl_car_layout) {
            startActivityForResult(PersonalCenterActivityConst.Path.CAR_MANAGER);
        } else if (id == R.id.ll_tel) {
            startActivityForResult(PersonalCenterActivityConst.Path.HOT_LINE);
        } else if (id == R.id.ll_system_setting) {
            startActivityForResult(PersonalCenterActivityConst.Path.SYSTEM_SETTING);
        } else if (id == R.id.ll_notice_setting) {
            startActivityForResult(PersonalCenterActivityConst.Path.NOTICE_SETTING);
        }
    }

}
